/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.krossk.dao;

import br.com.krossk.jpa.JPAFactory;
import br.com.krossk.model.Area;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Henrique
 */
public class AreaDAO {

    private static AreaDAO instance = new AreaDAO();

    public static AreaDAO getInstance(){
        return instance;
    }

    public List <Area> getAll(){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            Query query = em.createNamedQuery("Area.findAll");
            return query.getResultList();
        }finally{
            em.close();
        }
    }

    public Area getById(int idArea){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            return em.find(Area.class, idArea);
        }finally{
            em.close();
        }

    }

    public void update(Area a){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            em.getTransaction().begin();
            em.merge(a);
            em.getTransaction().commit();
        }finally{
            em.close();
        }
    }

    public void save(Area a){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            em.getTransaction().begin();
            em.persist(a);
            em.getTransaction().commit();
        }finally{
            em.close();
        }
    }

    public void delete(int idArea){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            Area a = em.find(Area.class, idArea);
            em.getTransaction().begin();
            em.remove(a);
            em.getTransaction().commit();
        }finally{
            em.close();
        }
    }
}
